<?php
/*
 * @Author: lokei
 * @Date: 2023-03-23 22:53:31
 * @LastEditors: lokei
 * @LastEditTime: 2023-09-14 10:40:10
 * @Description: 
 */
namespace App\Http\Controllers\Platform\Brand;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Platform\Brand\BrandModel;
use Illuminate\Http\Request;

class BrandController extends Controller {
    public function list(Request $request) {
        if($request->input('limit') != null && intval($request->input('limit')) > 0) {
            $items = BrandModel::limit($request->input('limit'))->get();
        } else {
            $items = BrandModel::get();
        }
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        $res['data']['total'] = count($items);
        return $res;
    }

    public function ofType0(Request $request) {
        $items = BrandModel::where('type_0_id', '=', $request->input('type_0_id'))->get();
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        return $res;
    }

    public function create(Request $request) {
        $brand = new BrandModel();
        $brand->type_0_id = $request->input('type_0_id');
        $brand->name = $request->input('name');
        $brand->logo = $request->input('logo');
        $brand->save();
        return ResultTool::success();
    }
    
    public function update(Request $request) {
        $brand = BrandModel::where('id', '=', $request->input('id'))->first();
        $brand->type_0_id = $request->input('type_0_id');
        $brand->name = $request->input('name');
        $brand->logo = $request->input('logo');
        $brand->save();
        return ResultTool::success();
    }

    public function delete(Request $request) {
        BrandModel::where('id', '=', $request->input('id'))->delete();
        return ResultTool::success();
    }
    
    public function setOrder(Request $request) {
        $article = BrandModel::where('id', '=', $request->input('id'))->first();
        $article->order_index = $request->input('order_index');
        $article->save();
        return ResultTool::success();
    }
}